package com.android.vgongyi.common;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.android.image.CacheTask;
import com.android.image.TPManager;
import com.android.vgongyi.R;
import com.android.vgongyi.entity.CommonEntity;
import com.android.vgongyi.utils.StringUtil;
import com.android.vgongyi.utils.SystemUtil;
import com.ant.liao.GifView;

public class WeiboAdapter extends BaseAdapter{
    private ArrayList<CommonEntity> commonList;
    private Context context;
    private LayoutInflater mLayoutInflater;
    private TPManager tm ;
    
    public WeiboAdapter(ArrayList<CommonEntity> commonList,Context ctx)
    {
    	this.commonList		 = commonList;
    	this.context		 = ctx;
    	this.mLayoutInflater = LayoutInflater.from(ctx);
    	this.tm				 = TPManager.getInstance();
    }
    
	public int getCount() {
		return commonList.size();
	}

	public Object getItem(int position) {
		return commonList.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		final CommonEntity tempCommonEntity = commonList.get(position);
		ViewHolder holder = null;
		if(convertView == null){
			
			holder =  new ViewHolder();
			convertView = mLayoutInflater.inflate(R.layout.item_home, null);
			holder.image_add_to_favourate=(LinearLayout) convertView.findViewById(R.id.add_to_favourate);
			holder.image_dispatch=(LinearLayout) convertView.findViewById(R.id.dispatch);
			holder.image_face=(ImageView) convertView.findViewById(R.id.image_face);
			holder.image_face_vertify = (ImageView)convertView.findViewById(R.id.image_face_vertify);
			holder.image_vie_comments=(LinearLayout) convertView.findViewById(R.id.check_comments);
			holder.image_detail_main = (ImageView)convertView.findViewById(R.id.weibo_img);
			holder.text__remark=(TextView) convertView.findViewById(R.id.text__remark);
			holder.text_dispatch=(TextView) convertView.findViewById(R.id.text_dispatch);
			holder.text_favourate=(TextView) convertView.findViewById(R.id.text_favourate);
			holder.text_image_vie_comments=(TextView) convertView.findViewById(R.id.text_image_vie_comments);
			holder.text_main=(TextView) convertView.findViewById(R.id.text_main);
			holder.text_name=(TextView) convertView.findViewById(R.id.text_name);
			holder.text_time_stamp=(TextView) convertView.findViewById(R.id.text_time_stamp);
			holder.text_website_logo=(ImageView) convertView.findViewById(R.id.text_website_logo);
			holder.weibo_img_layout=(LinearLayout) convertView.findViewById(R.id.weibo_img_layout);
			holder.layout_user_img = (FrameLayout) convertView.findViewById(R.id.layout_user_img);
			holder.gifView = (GifView) convertView.findViewById(R.id.weibo_gifview);
			holder.check_comments = (LinearLayout) convertView.findViewById(R.id.check_comments);
			
			
			convertView.setTag(holder);
        } else {
        	
        	holder = (ViewHolder) convertView.getTag();
        	
        }
			//ensure mid button text
		
			if(tempCommonEntity.isFavourite()){
				holder.text_favourate.setText(R.string.favorite_done);
			}else{
				holder.text_favourate.setText(R.string.favorite_add);
			}
		
			//set listener
			OnClickListener listener  =  new WeiboAdapterListener(context, this, tempCommonEntity);
			holder.image_add_to_favourate.setOnClickListener(listener);
			holder.image_vie_comments.setOnClickListener(listener);
			holder.image_detail_main.setOnClickListener(listener);
			holder.image_dispatch.setOnClickListener(listener);
			holder.layout_user_img.setOnClickListener(listener);
			holder.gifView.setOnClickListener(listener);
			
			//ensure left button and mid button visibility
			if(!tempCommonEntity.getWeibo_id().equals(AppConstant.TAG_AD)){
				holder.image_add_to_favourate.setVisibility(View.VISIBLE);
				holder.image_dispatch.setVisibility(View.VISIBLE);
			}else {
				holder.image_add_to_favourate.setVisibility(View.INVISIBLE);
				holder.image_dispatch.setVisibility(View.INVISIBLE);
			}
			//ensure profile vertify
			String imageUrl = tempCommonEntity.getProfile_imageurl();
			String imageVertifyUrl = tempCommonEntity.getVerified();
			if (imageVertifyUrl.equals("1")) {
				holder.image_face_vertify.setVisibility(View.VISIBLE);
			}
			//load profile image
			holder.image_face.setTag(imageUrl);
			tm.addTask(new CacheTask(imageUrl, holder.image_face));
	        
	        //load weibo image
	        String imageUrl2 = tempCommonEntity.getImg_url();
	        holder.image_detail_main.setBackgroundResource(R.drawable.loading_image);
			if(imageUrl2!=null && !imageUrl2.equals("")){
		     // 放大图片
				float big_h = Float.valueOf(tempCommonEntity.getBig_height());
				float big_w = Float.valueOf(tempCommonEntity.getBig_width());
				LayoutParams params = new LinearLayout.LayoutParams(0, 0);
				if (big_w != 0.0) {
					params = new LinearLayout.LayoutParams(SystemUtil.getImageWidthSize(context),SystemUtil.getImageHeightSize(context, big_h, big_w));
				}
				holder.weibo_img_layout.setLayoutParams(params);
			    holder.image_detail_main.setTag(imageUrl2);
			    tm.addTask(new CacheTask(imageUrl2, holder.image_detail_main));

			}else{
				LayoutParams params = new LinearLayout.LayoutParams(0,0);
				holder.weibo_img_layout.setLayoutParams(params);
			}
			
			//name
			holder.text_name.setText(tempCommonEntity.getWriter_name());
			//Verified_reason
			holder.text__remark.setText(tempCommonEntity.getVerified_reason());
			//date
			String spaceTime = StringUtil.getSpaceTime(tempCommonEntity.getInsert_date());
			holder.text_time_stamp.setText(String.format(context.getResources().getString(R.string.from),spaceTime));
			//weibo logo
			if(!tempCommonEntity.getWeibo_id().equals(AppConstant.TAG_AD)){ 
				holder.text_website_logo.setVisibility(View.VISIBLE);
				holder.text_website_logo.setImageResource(weiboPlatform(tempCommonEntity.getWeibo_platform()));
			}else { 
				holder.text_website_logo.setVisibility(View.INVISIBLE);
			}
			//weibo content
			holder.text_main.setText(StringUtil.toDBC(tempCommonEntity.getContent()));
			holder.check_comments.setVisibility(View.VISIBLE);
			//right button
			if(!tempCommonEntity.getWeibo_id().equals(AppConstant.TAG_AD)){
				if(null == tempCommonEntity.getComment_nums() || tempCommonEntity.getComment_nums().equals("")){
					holder.text_image_vie_comments.setText(String.format(context.getResources().getString(R.string.check_comment),"0"));   
				}else{
					holder.text_image_vie_comments.setText(String.format(context.getResources().getString(R.string.check_comment),tempCommonEntity.getComment_nums()));
				}
			}else{
				if(tempCommonEntity.getIs_showbtn().equals("1")){
					holder.text_image_vie_comments.setText(tempCommonEntity.getAd_btntitle());   
				}else{
					holder.check_comments.setVisibility(View.GONE);
				}
			}
	    return convertView;
	}
	
	private  class ViewHolder {
	     public ImageView image_face;
	     public ImageView image_face_vertify;
	     public TextView text_name;
	     public TextView text__remark;
	     public TextView text_time_stamp;
	     public ImageView text_website_logo;
	     public TextView text_main;
	     public ImageView image_detail_main;
	     public LinearLayout image_dispatch;
	     public TextView text_dispatch;
	     public LinearLayout image_add_to_favourate;
	     public TextView text_favourate;
	     public LinearLayout image_vie_comments;
	     public TextView text_image_vie_comments;
	     public LinearLayout weibo_img_layout;
	     public FrameLayout layout_user_img;
	     public GifView gifView;
	     public LinearLayout check_comments;
	}
	

	public int weiboPlatform(String platform){
		int imageResource;
		if(platform.equals(AppApiPreference.weiboPlatform[0])){
			imageResource = R.drawable.weibosinasmall;
		}else if(platform.equals(AppApiPreference.weiboPlatform[1])){
			imageResource = R.drawable.weibotencentsmall;
		}else if(platform.equals(AppApiPreference.weiboPlatform[3])){
			imageResource = R.drawable.weibo163small;
		}else{
			imageResource = R.drawable.weibosohusmall;
		}
		
		return imageResource;
	}
		
}
